home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group94a.txt / 000207_icon-group-sender _Mon Aug 15 14:30:52 1994.msg < prev    next >
Internet Message Format  |  1994-08-19  |  2KB

  1. Received: by cheltenham.cs.arizona.edu; Mon, 15 Aug 1994 15:31:07 MST
  2. Date: Mon, 15 Aug 1994 14:30:52 +0700
  3. From: swampler@noao.edu
  4. Message-Id: <9408152130.AA22645@orpheus.gemini.edu>
  5. Subject: Re: Generators
  6. To: icon-group@cs.arizona.edu
  7. Content-Length: 1261
  8. Status: R
  9. Errors-To: icon-group-errors@cs.arizona.edu
  10.  
  11. espie@ens.fr wrote:
  12.  
  13.  
  14. >>In Icon, you differentiate the other way. The implicit state differentiates
  15. >>where the function was called. If you want an expression that can be resumed
  16. >>elsewhere, you encapsulate it into a coexpression. Here is the classic 
  17. >>`build identifiers on the fly' in Icon.
  18. >>procedure newid
  19. >>    i := 0
  20. >>    repeat
  21. >>        {
  22. >>        i +:= 1
  23. >>        suspend "a"||i
  24. >>        }
  25. >>end
  26. >>
  27. >>called thus:
  28. >>gen := create newid()
  29. >>id:= @gen
  30. >>each time you wish for a new id.
  31.  
  32. Just to extend Mark's explanation a bit - there is no requirement that the
  33. expression 'encapsulated' by a coexpression consist of just a function call,
  34. one may capture *any* Icon expression.  So an alternative 'build identifiers
  35. on the fly' implementation is simply:
  36.  
  37.     gen := create "a"||seq()
  38.  
  39. since seq() is a function whose default behavior matches that
  40. provided by the counter in Mark's newid() procedure.  If gen is a global
  41. variable, then @gen will produce the next 'id' whenever and wherever it
  42. is invoked.
  43.  
  44. More on 'seq()' - it produces an infinite sequence of results on demand: 
  45.  
  46. {1,2,3...}
  47.  
  48. though you can provide arguments to set both the initial value and the step
  49. size, so 
  50.  
  51.     seq(2,2) produces {2,4,6,8,...}
  52.  
  53.  
  54. --
  55. Steve Wampler
  56. swampler@gemini.edu
  57. Gemini Project (under AURA)
  58.